package com.demo.nio.client;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CustomTextFrameHandler extends
    SimpleChannelInboundHandler<TextWebSocketFrame> {

  public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) throws Exception {
    Channel channel = ctx.channel();
    log.info(channel.remoteAddress() + " 发送的弹幕是：" + frame.text());
    for (Channel channel1 : channels) {
      if (channel1 != channel) {
        channel1.writeAndFlush(new TextWebSocketFrame(frame.text()));
      } else {
        channel1.writeAndFlush(new TextWebSocketFrame("我发送的:" + frame.text()));
      }
    }
  }

  @Override
  public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    channels.writeAndFlush(new TextWebSocketFrame("[SERVER]- " + channel.remoteAddress()) + "加入");
    channels.add(channel);
    log.info("Client:" + channel.remoteAddress() + "加入");
  }

  @Override
  public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    channels.writeAndFlush(new TextWebSocketFrame("[SERVER]- " + channel.remoteAddress()) + "离开");
    log.info("Client:" + channel.remoteAddress() + "离开");
  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    log.info("Client:" + channel.remoteAddress() + "在线");
  }

  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    log.info("Client:" + channel.remoteAddress() + "掉线");
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    Channel channel = ctx.channel();
    log.info("Client:" + channel.remoteAddress() + "异常");
    cause.printStackTrace();
    ctx.close();
  }
}
